﻿using DBUtil.SqlSegment;

namespace DBUtil.Provider.SqlServer.SqlSegment;

/*     
-- 字符串转小数
select convert(decimal(30,10),'123.123') -- "123.1230000000"
-- 字符串转整数
select convert(decimal,'123') -- "123"
-- 字符串转日期
select convert(datetime2,'2022-06-10 11:24:01.123') -- "2022-06-10 11:24:01.1230000"


-- 整数转字符串
select convert(varchar,123) --"123"
-- 小数转字符串
select convert(varchar,123.123) -- "123.123"
-- 日期转字符串 不带时区
select convert(varchar,getdate(),121) -- "2022-06-10 11:36:42.323"
*/

public class SqlServerConvertSqlSegment : ConvertSqlSegment
{
    public SqlServerConvertSqlSegment(DBAccess db) : base(db) { }

    private string ConvertTo(string sqlSeg, string toType) => $"convert({sqlSeg},{toType})";
    public override string ConvertDateTimeToString(string sqlSeg) => ConvertTo(sqlSeg, "char");
    public override string ConvertDecimalToString(string sqlSeg) => ConvertTo(sqlSeg, "char");
    public override string ConvertIntegerToString(string sqlSeg) => ConvertTo(sqlSeg, "char");
    public override string ConvertStringToDateTime(string sqlSeg) => ConvertTo(sqlSeg, "datetime(6)");
    public override string ConvertStringToDecimal(string sqlSeg) => ConvertTo(sqlSeg, "decimal(30,15)");
    public override string ConvertStringToInteger(string sqlSeg) => ConvertTo(sqlSeg, "decimal");
    public override string ConvertAllToString(string sqlSeg) => ConvertTo(sqlSeg, "char");
}
